#include<stdio.h>
#include<stdlib.h>

struct ListNode 
{
    int val;
    struct ListNode* next;
    
};

struct ListNode* removeElements(struct ListNode* head, int val) {
    struct ListNode* cur = head;
    struct ListNode* prev = NULL;
    while (cur != NULL)
    {
        if (cur->val != val)
        {
            prev = cur;
            cur = cur->next;
        }
        else
        {
            if (prev == NULL)
            {
                head = cur->next;
                free(cur);
                cur = head;
            }
            else
            {
                prev->next = cur->next;
                free(cur);
                cur = prev->next;
            }
        }
    }
    return head;
}

int main()
{
    struct ListNode* s1 = (struct ListNode*)malloc(sizeof(struct ListNode));
    struct ListNode* s2 = (struct ListNode*)malloc(sizeof(struct ListNode));
    struct ListNode* s3 = (struct ListNode*)malloc(sizeof(struct ListNode));
    struct ListNode* s4 = (struct ListNode*)malloc(sizeof(struct ListNode));

    s1->val = 7;
    s2->val = 7;
    s3->val = 7;
    s4->val = 7;

    s1->next = s2;
    s2->next = s3;
    s3->next = s4;
    s4->next = NULL;
    struct ListNode* s = s1;

    struct ListNode* pc = removeElements(s, 7);
}
